
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Text.RegularExpressions;

namespace Swaf
{
	/// <summary>
	/// Given a user agent string from the browser, this class will attempt to parse the 
	/// information in it to get Windows and IE verion numbers.
	/// </summary>
	public class UserAgent
	{
		public UserAgent(string agentInfo)
		{
			try{
				m_ieVersion = float.Parse(Regex.Match(agentInfo, @"MSIE\s+([^;]+);").Groups[1].ToString());
			}catch(Exception){} //Exception finding ie version means default value is used.
			
			try{
				string winver = Regex.Match(agentInfo, @"windows\s+([^;)]+)[;)]", RegexOptions.IgnoreCase).Groups[1].ToString();
				int indexPos;
				if((indexPos = winver.IndexOf("NT")) != -1)
				{
					float ver = float.Parse(Regex.Match(winver, @"([\d.]+)").Groups[1].ToString());
					if(ver >= 5.1F)
						m_winVersion = "XP";
					else if(ver == 5)
						m_winVersion = "2000";
					else
						m_winVersion = "NT";
				}
				else if(winver.IndexOf("98") != -1)
					m_winVersion = "98";
			}catch(Exception){}

			try{
				m_dotNetVersion = Regex.Match(agentInfo, @"\.NET\s+CLR\s+([^;)]+)[;)]", RegexOptions.IgnoreCase).Groups[1].ToString();
			}catch(Exception){}
		}

		public float ieVersion
		{get{
			return m_ieVersion;
		}}

		public string winVersion
		{get{
			 return m_winVersion;
		}}

		public string dotNetVersion
		{get{
			 return m_dotNetVersion;
		}}

		private float m_ieVersion = 0F;
		private string m_winVersion = "";
		private string m_dotNetVersion = "";

	}
}
